package network.okhttp.Post7_4;

import okhttp3.*;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * 爬取北京大学的课表并打印结果
 * 采用异步和同步操作
 */
public class PageOperator {
    private OkHttpClient okHttpClient;
    public PageOperator(){
        okHttpClient = new OkHttpClient();
    }
    //异步
    public String postContentAsync(String url, Map<String ,String>params){

        if (StringUtils.isBlank(url)){
            return null;
        }
        //post方式提交的数据
        FormBody.Builder builder = new FormBody.Builder();
        for (String key:params.keySet()) {
            builder.add(key,params.get(key));
        }
        FormBody formBody = builder.build();
        final Request request = new Request.Builder().url(url).post(formBody).build();
        //创建/Call
        Call call = okHttpClient.newCall(request);
        final  String requestUrl = url;
        //加入队列 异步操作
        call.enqueue(new Callback() {
            @Override
            public void onFailure(@NotNull Call call, @NotNull IOException e) {
                System.out.println("连接失败");
            }

            @Override
            public void onResponse(@NotNull Call call, @NotNull Response response) throws IOException {
                //5.获得网络数据
                if (response.code() == 200) {
                    String result = response.body().string();
                    System.out.println("call "+requestUrl+"content's size is" +result.length());
                    System.out.println(result);
                }

            }
        });

        return "success";
    }
    //同步
    public String postContentsync(String url, Map<String ,String>params){

        if (StringUtils.isBlank(url)){
            return null;
        }
        //post方式提交的数据
        FormBody.Builder builder = new FormBody.Builder();
        for (String key:params.keySet()) {
            builder.add(key,params.get(key));
        }
        FormBody formBody = builder.build();
       // final Request request = new Request.Builder().url(url).post(formBody).build();
        final Request request = new Request.Builder().url(url).build();
        //创建/Call
        Call call = okHttpClient.newCall(request);
        final  String requestUrl = url;
        //加入队列 异步操作
        String result = null;
        try {
            result = call.execute().body().string();
            System.out.println("call "+requestUrl+"content's size is" +result.length());
        } catch (IOException e) {
            e.printStackTrace();
        }

        return result;
    }

    public String postContent(String url, Map<String, String> formData) {
        //post方式提交的数据
        FormBody.Builder builder = new FormBody.Builder();
        // 放入表单数据
        for (String key : formData.keySet()) {
            builder.add(key, formData.get(key));
        }
        // 构建 FormBody 对象
        FormBody formBody = builder.build();
        // 指定 post 方式提交FormBody
        Request request = new Request.Builder()
                .url(url)
                .post(formBody)
                .addHeader("User-Agent",
                        "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_1) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/78.0.3904.108 Safari/537.36")
                .addHeader("Cookie","PHPSESSID=d5c6t316o9ffcdj0igiftl0251")
                .addHeader("Referer","http://www.houqinbao.com/hydropower/index.php?m=PayWeChat&c=Index&a=before&payopenid=&openid=oUiRowVf9LEm212UhpyCfXVjl4GM&schoolcode=13579&campusid=13579_173&areaid=13579_173_288&flatid=13579_173_288_949&roomname=A7092")
                .addHeader("Host","www.houqinbao.com")
                .build();

        // 返回结果字符串
        String result = null;
        try {
            result = okHttpClient.newCall(request).execute().body().string();
            System.out.println("result:"+result.length());
        } catch (IOException e) {
            System.out.println("request " + url + " error . ");
            e.printStackTrace();
        }
        return result;
    }


    public static void main(String[] args) {
        PageOperator pageOperator = new PageOperator();
        Map<String,String> params = new HashMap<>();
          params.put("flatname","杏2楼");
          params.put("roomname","A7092");
//        params.put("xn", "19-20");
//        params.put("xq", "1");
//        params.put("xs", "011");
//        params.put("nj", "18");
         String result = pageOperator.postContent("http://www.houqinbao.com/hydropower/index.php?m=PayWeChat&c=IndexKd&a=find&schoolcode=13579",params);
         System.out.println(result);
        System.out.println("hello");

    }
}
